$(document).ready(function(){
    
    $('#credit_register_number').select2({
                minimumInputLength: 2,
                ajax: {
                    url: "/debtor/findByAjax/",
                    dataType: 'json',
                    data: function (term, page) {
                        return {
                            q: term
                          };
                    },
                    results: function (data, page) {                        
                        return {
                            results: data
                        };
                    }
                }
            });
            
    if(newRegisterNumber !== null)
    {
        $("#s2id_credit_register_number").find('span').html(newRegisterNumber);
        $("#credit_register_number").val(newRegisterNumber);
    }
            
});
////    $(".select2-input").keyup(function(){
////        
////        var registerNumber = $(this).val();
////        if(registerNumber.length > 0){
////            
////            $.ajax({
////                type: 'post',
////                url: '/debtor/findByAjax/',
////                data: {
////                    registerNumber: registerNumber
////                },
////                dataType: "JSON",
////                beforeSend: function(){
////                    $(".select2-results").html('');
////                },
////                success: function (data) {
////                    
////                    if(data.status === 'success')
////                    {
////                        
////                        $.each(data.result, function(i, row) {
////                        
////                           $(".select2-results").append('<li class="select2-results-dept-0 select2-result select2-result-selectable">\n\
////                                            <div class="select2-result-label">\n\
////                                            <span class="select2-match"></span>'+row.REGISTER_NUMBER+'\n\
////                                            </div>\n\
////                                          </li>');
////                            
////                            
////                        });
////                        
////                    }else{
////                        
////                    }
////                    
////                },
////                error: function () {
////                    
////                }
////            });
////
////            
////        }
////    });
//    
//    
//});

function calcTotalAmount()
{
    var credit_initial_amount = parseFloat($('#credit_initial_amount').val().replace(/,/g, ''));
    var credit_interest_amount = parseFloat($('#credit_interest_amount').val().replace(/,/g, ''));
    
    if(isNaN(credit_initial_amount))
    {
           credit_initial_amount = 0;
    }
    
    if(isNaN(credit_interest_amount))
    {
        credit_interest_amount = 0;
    }
    
    $('#credit_total_amount').val(credit_initial_amount + credit_interest_amount);
    $('#credit_total_amount').formatCurrency();
    
    changeTotalAmount();
}